package com.jbinfo.qingcaowan.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.jbinfo.qingcaowan.common.URLUtil;

public class LoginInterceptor extends LoginContextInterceptor {
	/**
	 * 被忽略不被安全检查的url
	 */
	private List<String> ignoralList = new ArrayList<String>();
	private String appPrefix = "/app";
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//设置忽略判断登录的url
		String currentUrl = request.getRequestURI();
		String actualUrl = StringUtils.defaultIfEmpty(currentUrl, "/");
		if (!actualUrl.startsWith("/")) {
			actualUrl = "/" + actualUrl;
		}
		// 添加AngularJs 跨域解决方法
		response.setHeader("Access-Control-Allow-Origin" 
				,"*"); // http://my.dj.com   http://www.dotnetdev.net
		
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods"
				,"PUT, GET, POST, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers"
				,"X-Requested-With,Content-Type,Authorization,Accept,Range,Origin");
		response.setHeader("Access-Control-Expose-Headers"
				,"Content-Range");
		response.setHeader("Access-Control-Max-Age","3600");
				
		if(!needCheckLogin(currentUrl)){
			return true;
		}
		//如果非忽略的url，且没有登录，将根据不同的客户端进行不同判断
//		LoginContext loginContext = LoginContextUtil.getLoginContext(request.getSession());
		//pc端将跳转到登录页，app端将返回没有登录 标识
//		if(loginContext == null){
//			if(actualUrl.startsWith(appPrefix)){
//				response.setContentType("application/json;charset=UTF-8");
//				ViewResult result = new ViewResult();
//				result.setStatus(ResultCode.NO_LOGIN);
//				result.setResult("还没有登录哦 ");
//				//response.getWriter().write(JsonMapper.JSON_NON_EMPTY_MAPPER.toJson(result));
//				response.getWriter().write(JSONObject.fromObject(result).toString());
//				return false;
//			}else{
//				request.getRequestDispatcher("/login").forward(request, response);
//				return true;
//			}
//		}
		
		//如果登录过，则进行下一步操作
		return super.preHandle(request, response, handler);
	}
	
	/** 
	* @Description: 验证是否需要验证登录
	* @author Liu Wenjie   
	* @date 2014-11-13 下午8:16:17 
	* @return  
	*/ 
	private boolean needCheckLogin(String url){
		String actualUrl = StringUtils.defaultIfEmpty(url, "/");
		if (!actualUrl.startsWith("/")) {
			actualUrl = "/" + actualUrl;
		}
		
		for (String resource : ignoralList) {
			if (actualUrl.startsWith(resource) || URLUtil.authenticatorUrl(actualUrl,resource)) {
				return false;
			}
		}
		
		return true;
	}
	
	/*==============getter and setter =================*/

	/**
	 * @Description: 属性 ignoralList 的set方法 
	 * @param ignoralList 
	 */
	public void setIgnoralList(List<String> ignoralList) {
		this.ignoralList = ignoralList;
	}

	/**
	 * @Description: 属性 appPrefix 的set方法 
	 * @param appPrefix 
	 */
	public void setAppPrefix(String appPrefix) {
		this.appPrefix = appPrefix;
	}
	
}
